package com.hfjyz.registration.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import com.hfjyz.registration.command.OrderInfoSearchCommand;
import com.hfjyz.registration.contants.ExamContants;
import com.hfjyz.registration.model.Exam;
import com.hfjyz.registration.model.User;
import com.hfjyz.registration.service.OrderInfoService;

public class OrderInfoSearchController extends SimpleFormController {
	private OrderInfoService orderInfoService;

	public OrderInfoService getOrderInfoService() {
		return orderInfoService;
	}

	public void setOrderInfoService(OrderInfoService orderInfoService) {
		this.orderInfoService = orderInfoService;
	}

	@SuppressWarnings("rawtypes")
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		OrderInfoSearchCommand cmd = (OrderInfoSearchCommand) command;
		Exam exam = (Exam) request.getSession().getAttribute(
				ExamContants.SESSION_KEY_EXAM_INFO);
		cmd.setExam(exam);
		Date now = Calendar.getInstance().getTime();
		if (exam.getPayStartDate() == null || now.before(exam.getPayStartDate())) {
			throw new Exception("尚未到支付时间，请等待！");
		} else if (exam.getPayEndDate() == null || now.after(exam.getPayEndDate())) {
			throw new Exception("很抱歉，支付已经结束！");
		}
		return null;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		OrderInfoSearchCommand cmd = (OrderInfoSearchCommand) command;
		Exam exam = (Exam) request.getSession().getAttribute(
				ExamContants.SESSION_KEY_EXAM_INFO);
		cmd.setExam(exam);
		String submitType = cmd.getSubmitType();
		Map model = errors.getModel();
		if ("search".equals(submitType)) {
			orderInfoService.loggin(request, ExamContants.LOGING_TYPE_INFO,
					(Integer) null, ExamContants.LOGING_USER_TYPE_USER,
					cmd.getIdCardNo(), ExamContants.LOGING_OP_TYPE_SEARCH,
					"支付状态查询");
			cmd.setShowDetail(false);
			if (!errors.hasErrors()) {
				orderInfoService.searchOrderInfo(cmd, errors);
			}
			if (errors.hasErrors()) {
				return new ModelAndView(getFormView(), errors.getModel());
			}
			HttpSession session = request.getSession();
			session.setAttribute("UserInfo", cmd.getUser());
			cmd.setShowDetail(true);
			request.getSession().setAttribute("photoData" + cmd.getIdCardNo(),
					cmd.getUser().getPhoto());
			return new ModelAndView(getFormView(), errors.getModel());
		} else {
			HttpSession session = request.getSession();
			User user = (User) session.getAttribute("UserInfo");

			RedirectView rv = new RedirectView("orderInfoPay.action");
			rv.setExposeModelAttributes(true);
			model.put("userId", user.getId());
			return new ModelAndView(rv, model);
		}
	}
}
